# dialogCenterUtils.py

def center_window(window, width, height):
    """居中显示窗口在屏幕中央"""
    screen_width = window.winfo_screenwidth()
    screen_height = window.winfo_screenheight()
    x = (screen_width // 2) - (width // 2)
    y = (screen_height // 2) - (height // 2)
    window.geometry(f"{width}x{height}+{x}+{y}")


def center_dialog(dialog, parent, width, height):
    """将弹窗居中显示在父窗口正中央"""
    dialog.update_idletasks()

    # 获取父窗口的屏幕坐标和尺寸
    px = parent.winfo_rootx()
    py = parent.winfo_rooty()
    pw = parent.winfo_width()
    ph = parent.winfo_height()

    # 计算居中坐标（考虑父窗口中心点）
    x = px + (pw - width) // 2
    y = py + (ph - height) // 2

    # 设置窗口大小与位置
    dialog.geometry(f"{width}x{height}+{x}+{y}")

